package com.aranandroid.customview.progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.aranandroid.customview.R;


/**
 * Created by Chen on 2017/6/4.
 * GPS信号控件
 */

public class GpsSignlView extends View {


    /**
     * 默认View宽度
     */
    private static final int DEFAULT_VIEW_WIDTH = 56;
    /**
     * 默认GPS点高
     */
    private static final int DEFAULT_VIEW_HEIGHT = 152;

    /**
     * 默认GPS点宽度
     */
    private static final int DEFAULT_GPS_WIDTH = 6;
    /**
     * 默认GPS点高
     */
    private static final int DEFAULT_GPS_HEIGHT = 10;

    /**
     * 默认的进度字体大小
     */
    private static final int DEFAULT_TEXT_SIZE = 12;

    // 信号大小 GPS信号组件实际位置，上面两个只作样式展示。
    //显示规则参考：共9格，3格以下显示为信号弱样式，4-6格为信号中等样式，7-9格为信号强样式
    private int mSignl = 0;

    @ColorInt
    private int bg;
    @ColorInt
    private int textColor;

    //文字的大小
    private int mTextSize;
    // 控件宽高
    private int mViewHeight;
    private int mViewWidth;
    // 内部点宽高
    private int mGpsHeight;
    private int mGpsWidth;


    // 长方形的画笔
    private Paint mRectanglePaint;
    private Paint mGpsPaint;
    // 画文字的笔
    private Paint mTextUpPaint;
    private Paint mTextBottomPaint;
    // 文字的区域
    private Rect mTextUpRect;
    private Rect mTextBottomRect;


    public GpsSignlView(Context context) {
        this(context, null);
    }

    public GpsSignlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GpsSignlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(attrs);
        initPaint();
    }

    private void getAttrs(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.GpsSignlView);
        bg = ta.getColor(R.styleable.GpsSignlView_bg, Color.parseColor("#FFFFFF"));
        textColor = ta.getColor(R.styleable.GpsSignlView_textColor, Color.parseColor("#000000"));
        mTextSize = ta.getDimensionPixelSize(R.styleable.GpsSignlView_textSize, sp2px(DEFAULT_TEXT_SIZE));
        mGpsHeight = ta.getDimensionPixelSize(R.styleable.GpsSignlView_gpsHeight, sp2px(DEFAULT_GPS_HEIGHT));
        mGpsWidth = ta.getDimensionPixelSize(R.styleable.GpsSignlView_gpsWidth, sp2px(DEFAULT_GPS_WIDTH));
        mViewHeight = ta.getDimensionPixelSize(R.styleable.GpsSignlView_viewHeight, sp2px(DEFAULT_VIEW_HEIGHT));
        mViewWidth = ta.getDimensionPixelSize(R.styleable.GpsSignlView_viewWidth, sp2px(DEFAULT_VIEW_WIDTH));
        ta.recycle();
    }


    private void initPaint() {
        mTextUpPaint = new Paint();
        // 头部文字
        //   #FF6363  #FFB300  #2BBC3A 颜色
        mTextUpPaint.setColor(Color.parseColor("#2BBC3A"));
        // 字体大小
        mTextUpPaint.setTextSize(mTextSize);
        // 加粗
        mTextUpPaint.setTypeface(Typeface.DEFAULT_BOLD);
        // 底部文字 画笔
        mTextBottomPaint = new Paint();
        mTextBottomPaint.setColor(textColor);
        mTextBottomPaint.setTextSize(mTextSize);
        mTextBottomPaint.setTypeface(Typeface.DEFAULT_BOLD);
        // 矩形画笔
        mRectanglePaint = new Paint();
        mRectanglePaint.setColor(bg);
        mRectanglePaint.setStyle(Paint.Style.FILL);
        mRectanglePaint.setAntiAlias(true);
        // gps矩形画笔
        mGpsPaint = new Paint();
        mGpsPaint.setColor(Color.parseColor("#ddE8E8E8"));
        mGpsPaint.setStyle(Paint.Style.FILL);
        mGpsPaint.setAntiAlias(true);
        // rect
        mTextUpRect = new Rect();
        mTextBottomRect = new Rect();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //控件宽高
        setMeasuredDimension(mViewWidth, mViewHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 根据信号大小处理文字颜色
        int gpsColor = Color.parseColor("#FF6363");
        String gpsText = "弱";
        switch (mSignl) {
            case 4:
            case 5:
            case 6:
                gpsColor = Color.parseColor("#FFB300");
                gpsText = "中等";
                break;
            case 7:
            case 8:
            case 9:
                gpsColor = Color.parseColor("#2BBC3A");
                gpsText = "强";
                break;
        }
        //   #FF6363  #FFB300  #2BBC3A
        mTextUpPaint.setColor(gpsColor);

        //画矩形
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawRoundRect(0, 0, mViewWidth, mViewHeight, dp2px(4), dp2px(4), mRectanglePaint);
        } else {
            canvas.drawRect(0, 0, mViewWidth, mViewHeight, mRectanglePaint);
        }

        mTextBottomPaint.getTextBounds("GPS", 0, "GPS".length(), mTextBottomRect);
        canvas.drawText("GPS", (mViewWidth - mTextBottomRect.width()) / 2,
                mViewHeight - mTextBottomRect.height(), mTextBottomPaint);

        mTextUpPaint.getTextBounds(gpsText, 0, gpsText.length(), mTextUpRect);
        canvas.drawText(gpsText, (mViewWidth - mTextUpRect.width()) / 2,
                mTextUpRect.height() * 3 / 2, mTextUpPaint);

        //画矩形
        // 最后一个gps矩形 位置
        int leftMid = (mViewWidth - mGpsWidth) / 2;
        int topMid = (mViewHeight - mGpsHeight) / 2 + (4 * mGpsHeight + dp2px(1)) + dp2px(3);
        int rightMid = (mViewWidth + mGpsWidth) / 2;
        int bottomMid = (mViewHeight + mGpsHeight) / 2 + (4 * mGpsHeight + dp2px(1)) + dp2px(3);
        // 循环画gps矩形 画9个
        for (int i = 1; i <= 9; i++) {
            if (i > mSignl) {
                mGpsPaint.setColor(Color.parseColor("#E8E8E8"));
            } else {
                mGpsPaint.setColor(gpsColor);
            }
            canvas.drawRect(leftMid, topMid, rightMid, bottomMid, mGpsPaint);
            // 往上移一个矩形距离
            topMid = topMid - (mGpsHeight + dp2px(1));
            bottomMid = bottomMid - (mGpsHeight + dp2px(1));
        }


    }

    /**
     * 设置信号
     *
     * @param mSignl
     */
    public void setProgress(int mSignl) {
        this.mSignl = mSignl;
        postInvalidate();
    }

    //dp to px
    protected int dp2px(int dpval) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpval, getResources().getDisplayMetrics());
    }

    //sp to px
    protected int sp2px(int dpval) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dpval, getResources().getDisplayMetrics());
    }
}
